/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis;

import com.cxhd.lis.util.CacheTool;
import com.jfinal.plugin.activerecord.Config;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.DbKit;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.dialect.Dialect;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.activerecord.dialect.OracleDialect;
import com.jfinal.plugin.activerecord.dialect.SqlServerDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 52461
 */
public class DataCache {

    private static DataCache dataCache;

    private List<String> listAll = new ArrayList<String>();

    private Map<String, String> config = new HashMap<String, String>();

    private String localhostSN = System.getenv("COMPUTERNAME");

    private Map<String, List<String>> authMap = new HashMap<String, List<String>>();
    private DruidPlugin druid;

    public static DataCache getIntance() {

        if (dataCache == null) {
            dataCache = new DataCache();
        }
        return dataCache;
    }

    public void setUserAuth(String user_pk, List<String> list) {

        authMap.put(user_pk, list);
    }
 
    public List<String> getUserAuth(String user_pk,String role_id) {
        if( authMap.get(user_pk)==null){
        List<Record> listAuth = Db.find("select b.url,b.icon_class from lis_system_role_res a left join lis_system_res b on a.res_id=b.pk where role_id=" + role_id);
             CacheTool.saveAuthToCache(user_pk, listAuth);
        }
        return authMap.get(user_pk);
    }

    public void setAllAuth(List<String> list) {
        listAll = list;
    }

    public List<String> getAllAuth() {
        return listAll;
    }

    public Map<String, String> getConfig() {
        return config;
    }

    public void setConfig(Map<String, String> config) {
        this.config = config;
    }

    public void initConfigForHisDb() {

        if (config.get("hisDbFlag") != null && config.get("hisDbFlag").equalsIgnoreCase("true")) {
            String url = "";

            Dialect dialect = null;
            if (config.get("hisDbType").equalsIgnoreCase("mysql")) {
                url = "jdbc:mysql://" + config.get("hisDbUrl") + ":3306/" + config.get("hisDbName") + "?useUnicode=true&amp;zeroDateTimeBehavior=convertToNull";
                dialect = new MysqlDialect();
            } else if (config.get("hisDbType").equalsIgnoreCase("sqlserver")) {
                url = "jdbc:sqlserver://" + config.get("hisDbUrl").replace("\\\\", "\\").replace(",", ":") + ";databaseName=" + config.get("hisDbName");
                dialect = new OracleDialect();
            } else if (config.get("hisDbType").equalsIgnoreCase("mysql")) {
                url = "jdbc:oracle:thin:@//" + config.get("hisDbUrl") + ":1521/" + config.get("hisDbName");
            }
            if (druid != null) {
                druid.stop();
            }
            druid = new DruidPlugin(url, config.get("hisDbUser"), config.get("hisDbPassword"));
            druid.start();

            Config dbConfig = new Config("hisdb", druid.getDataSource(), dialect);

            DbKit.removeConfig("hisdb");
            DbKit.addConfig(dbConfig);

        }

    }

    public String getLocalhostSN() {
        return localhostSN;
    }

    public void setLocalhostSN(String localhostSN) {
        this.localhostSN = localhostSN;
    }

    public String getSelectHisSql(String PATIENTID) {
        int day = 3;
        if (DataCache.getIntance().getConfig().get("queryDay") != null) {
            day = Integer.parseInt(DataCache.getIntance().getConfig().get("queryDay"));
        }
        return "select a.Require_Date as CUSTOM1, c.Diagnosis as DIAGNOSTIC,c.Doctor as SJYS, b.Office as SECTION, a.Patient_Code as PATIENTID, a.Sample_No as BARCODE,a.Patient_name as PATIENTNAME,Case a.Patient_sex when '1' then '女' else '男' End AS SEX,a.Patient_Age as AGE,b.Item_code,b.Item_name as EXAMINAIM from Ut_Lis_patient_info a LEFT JOIN Ut_Patient_Cost b on a.Sample_No=b.Order_no LEFT JOIN Ut_Patient_Info c on a.Patient_Code=c.Patient_code where a.Patient_code like '%" + PATIENTID + "' and DateDiff(dd,b.Check_date,getdate())<=" + day;
    }
}
